package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
//    @Autowired
//    private RequestMappingHandlerMapping rmhm;

    public void saveOrUpdate(Permission entity) {
        if (entity.getId() == null) {
            permissionMapper.insert(entity);
        } else {
            permissionMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public List<Permission> selectByRoleId(Long roleId) {
        return permissionMapper.selectByRoleId(roleId);
    }

    @Override
    public void reload() {
//        // 获取 原来的 Permission
//        List<String> resources = permissionMapper.selectResources();
//        // 获取 所有 RequestMapping 注解 的 Method
//        Collection<HandlerMethod> methods = rmhm.getHandlerMethods().values();
//        for (HandlerMethod method : methods) {
//            RequiresPermissions anno = method.getMethodAnnotation(RequiresPermissions.class);
//            if (anno == null) {
//                continue;
//            }
//            String[] arr = anno.value();
//            String sn = arr[0];
//            String name = arr[1];
//            if (!resources.contains(sn)) {
//                Permission per = new Permission();
//                per.setName(name);
//                per.setResource(sn);
//                permissionMapper.insert(per);
//            }
//
//        }
    }

    @Override
    public List<String> selectByEmployeeId(Long employeeId) {
        return permissionMapper.selectByEmployeeId(employeeId);
    }


}
